package com.hankol.community.fragment

import androidx.lifecycle.Observer
import com.baselibrary.extension.checkNotNull
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.activity.BaseFragment
import com.baselibrary.bean.ReleaseTopicBean
import com.hankol.community.R
import com.hankol.community.adapter.DynamicSearchTopicAdapter
import com.hankol.community.bean.TopicClassifyBean
import com.hankol.community.databinding.FragmentDynamicSearchBinding
import com.hankol.community.viewmodel.DynamicSearchTopicViewModel

/**全部话题
 * @description
 * @author 关忆北、
 * @time 2022/4/20 4:05 下午
 */
class DynamicSearchTopicNewFragment(var recommendTags: TopicClassifyBean) : BaseFragment() {
    private lateinit var mDataBinding: FragmentDynamicSearchBinding
    private lateinit var mViewModel: DynamicSearchTopicViewModel
    private lateinit var adapter: DynamicSearchTopicAdapter
    private var topicList: List<ReleaseTopicBean.DataBean>? = null
    override fun init() {
        mDataBinding = getDataBinding<FragmentDynamicSearchBinding>()
        mViewModel = getFragmentViewModelProvider(this).get(DynamicSearchTopicViewModel::class.java)
        adapter = DynamicSearchTopicAdapter()
        mDataBinding.rvSearchTopicInfo.adapter = adapter
        showLoadingDialog()
        mViewModel.reqSearchTopic(recommendTags.id.toLong(), 1)
        mViewModel.searchTopicLD.observe(this, Observer {
            hideLoadingDialog()
            if (it.checkNotNull()) {
                topicList = it
                adapter.setNewData(it)
            }
        })
        adapter.setOnItemClickListener { adapter, view, position ->
            if (topicList.checkNotNull()) {
                val hashMap = HashMap<String, Any>()
                hashMap["tid"] = topicList?.get(position)?.id!!
                hashMap["tsource"] = "全部话题"
                ARouterUtils.jump(ActivityRouter.COMMUNITY.COMMUNITY_TOPIC_DETAIL, hashMap)
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_dynamic_search
    }

}